using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonPet : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_CommonPet
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExImage imageIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLv { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public UnityEngine.GameObject tagWearedState { protected set; get; }
            public UnityEngine.GameObject tagWearHero { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage iconWear { protected set; get; }
            public UnityEngine.GameObject select { protected set; get; }
            public UnityEngine.GameObject tagLock { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("imageIcon", out var __tbv0);
                this.imageIcon = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLv", out var __tbv1);
                this.textLv = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv2);
                this.textName = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagWearedState", out var __tbv3);
                this.tagWearedState = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagWearHero", out var __tbv4);
                this.tagWearHero = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("iconWear", out var __tbv5);
                this.iconWear = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("select", out var __tbv6);
                this.select = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagLock", out var __tbv7);
                this.tagLock = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region fields

        private Pet pet = null;
        private HeroData wearer = null;

        #endregion fields

        #region properties

        protected UIB_UIN_CommonPet ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonPet();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds



        public void Show(Pet pet, HeroData wearer = null)
        {
            this.SetData(pet, wearer);
            if (pet == null)
            {
                this.Hide();
                return;
            }

            this.Show();
            this.ShowIcon();
            this.ShowName();
            this.ShowLv();
            this.ShowWearState();
            this.ShowLock();
        }

        private void SetData(Pet pet, HeroData wearer)
        {
            this.pet = pet;
            this.wearer = wearer;
        }

        private void ClearData()
        {
            this.pet = null;
            this.wearer = null;
        }

        private void ShowIcon()
        {
            var cfg = this.pet.cfgData;
            this.SetImageSprite(this.ui.imageIcon, cfg.Icon_fullPath);
        }

        private void ShowName()
        {
            var name = this.pet.GetName();
            this.ui.textName.text = name;
        }

        private void ShowLv()
        {
            this.ui.textLv.text = this.pet.level.ToString("00");
        }

        private void ShowLock()
        {
            this.ui.tagLock.SetActive(this.pet.isLock);
        }

        private void ShowWearState()
        {
            var wearer = this.pet.currWearer;
            bool isAcvite = (wearer != null);
            bool isActiveOtherWearer = false;
            bool isActiveCurrWearer = false;
            if (isAcvite)
            {
                isActiveCurrWearer = wearer == this.wearer;
                isActiveOtherWearer = !isActiveCurrWearer;
            }
            if (isActiveOtherWearer)
            {
                this.ShowOtherHero(wearer);
            }
            this.SetOtherWearerActive(isActiveOtherWearer);
            this.SetCurrWearerActive(isActiveCurrWearer);
        }

        private void ShowOtherHero(HeroData otherWearer)
        {
            this.SetImageSprite(this.ui.iconWear, otherWearer.cfgSkin.HeroHeadImage_fullPath);
        }

        public void SetOtherWearerActive(bool isActive)
        {
            this.ui.tagWearHero.SetActive(isActive);
        }

        public void SetCurrWearerActive(bool isActive)
        {
            this.ui.tagWearedState.SetActive(isActive);
        }

        public void SetSelected(bool selected)
        {
            this.ui.select.SetActive(selected);
        }
        #endregion methonds
    }
}
